<?php

App::uses('Controller',    'Controller');

/**
 * Main Application Controller
 * @author Voislav Jovanovic
 */
class AppController extends Controller {
	/**
	 * @var array list of components used
	 */
	public $components = array(
        'Session',
		'Auth' => array(
			'loginAction' => array(
				'controller' => 'app', 
				'action'     => 'login'
			)
		)
	);
	
	protected function setFlash($message, $type='error') {
        $this->Session->setFlash($message, 'flash', array( 'type' => $type ));
	}
	
	/**
	 * Process login
	 */
	public function login() {
		$this->layout = 'login';
		
	    if ($this->request->is('post')) {
	        $data = $this->data['User'];
	        
	        if ($this->Auth->login()) {
	            return $this->redirect('/');
	        } else {
	            $this->setFlash('Login failed. &lt;CAPS-LOCK&gt; On?');
	        }
	    }
	}
	
	/**
	 * Process logout
	 */
	public function logout() {
	    return $this->redirect($this->Auth->logout());
	}
	
	/**
	 * Get currently logged in User/attribute
	 * @param string $attribute
	 */
	protected function getUser($attribute=null) {
		return $this->Auth->user($attribute);
	}
}
